//
//  MLISTwoView.m
//  BaseProject
//
//  Created by mac on 2021/1/11.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "MLISTwoView.h"
#import "iCarousel.h"
#import "MLIMoreVC.h"
@interface MLISTwoView ()<iCarouselDelegate,iCarouselDataSource>
@property(nonatomic,strong)UIImageView *topIV;
@property(nonatomic,strong)iCarousel *bannerIC;
@property(nonatomic,weak)NSTimer *bannerTimer;
@property(nonatomic,strong)NSArray<NSString *> *dataSource;
@property(nonatomic,strong)UIButton *lookButton;
@end

@implementation MLISTwoView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.viewDidAppear = YES;
        [self bannerIC];
        [self topIV];
        [self lookButton];
        self.height = self.bannerIC.bottom;
    }
    return self;
}
- (void)setViewDidAppear:(BOOL)viewDidAppear
{
    _viewDidAppear = viewDidAppear;
    if (self.dataSource.count < 2) {
        return;
    }
    if (viewDidAppear) {
        [self.bannerTimer invalidate];
        self.bannerTimer = nil;
        self.bannerTimer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(rollBannerIC) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop]addTimer:self.bannerTimer forMode:NSRunLoopCommonModes];
    } else {
        [self.bannerTimer invalidate];
        self.bannerTimer = nil;
    }
}
- (void)rollBannerIC
{
    [self.bannerIC scrollToItemAtIndex:(self.bannerIC.currentItemIndex + 1) animated:YES];
}
#pragma mark - iCarousel代理
- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return self.dataSource.count;
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view
{
    if (!view) {
        view = [[UIView alloc]initWithFrame:carousel.frame];
        
        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, view.width, view.height)];
        [view addSubview:iv];
        iv.tag = 100 + index;
    }
    UIImageView *iv = (UIImageView *)[view viewWithTag:100 + index];
    iv.image = [UIImage getPNGimageInBundleWithName:self.dataSource[index]];
    return view;
}
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"%ld",index);
}
- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    if (option == iCarouselOptionWrap)
    {
        return YES;
    }
    return value;
}
- (void)carouselCurrentItemIndexDidChange:(iCarousel *)carousel
{
    
}
- (void)carouselWillBeginDragging:(iCarousel *)carousel
{
    self.viewDidAppear = NO;
}
- (void)carouselDidEndDragging:(iCarousel *)carousel willDecelerate:(BOOL)decelerate
{
    self.viewDidAppear = YES;
}
#pragma mark - click
- (void)clickLookButton:sender
{
    MLIMoreVC *vc = [[MLIMoreVC alloc]init];
    [self.parentVC.navigationController pushViewController:vc animated:YES];
}
#pragma mark - lazy懒加载
- (UIImageView *)topIV
{
    if (!_topIV) {
        UIImageView *iv = [[UIImageView alloc]init];
        [self addSubview:iv];
        iv.image = [UIImage getPNGimageInBundleWithName:@"MLI_Segement_2"];
        iv.frame = CGRectMake(14*ADAPTER_WIDTH, 0, self.width - 28*ADAPTER_WIDTH, 507*ADAPTER_WIDTH);
        iv.userInteractionEnabled = YES;
        _topIV = iv;
    }
    return _topIV;
}
- (iCarousel *)bannerIC
{
    if (!_bannerIC) {
        iCarousel *ic = [[iCarousel alloc]initWithFrame:CGRectMake(14*ADAPTER_WIDTH, 507*ADAPTER_WIDTH - 20*ADAPTER_WIDTH, self.width - 28*ADAPTER_WIDTH, 475*ADAPTER_WIDTH)];
        [self addSubview:ic];
        ic.scrollSpeed = 1;
        ic.type = 0;
        ic.pagingEnabled = YES;
        ic.delegate = self;
        ic.dataSource = self;
        ic.clipsToBounds = YES;
        ic.layer.cornerRadius = 0*ADAPTER_WIDTH;
        ic.layer.masksToBounds = YES;
        _bannerIC = ic;
    }
    return _bannerIC;
}
- (NSArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = @[@"MLI_Segement_2_0",@"MLI_Segement_2_1"];
    }
    return _dataSource;
}
- (UIButton *)lookButton
{
    if (!_lookButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.topIV addSubview:button];
        [button addTarget:self action:@selector(clickLookButton:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, self.topIV.height - 100*ADAPTER_WIDTH, self.topIV.width, 50*ADAPTER_WIDTH);
        _lookButton = button;
    }
    return _lookButton;
}
@end
